layui.config({
    base: "static/layui/module/"
}).extend({
    dynamicCondition: 'dynamicCondition/dynamicCondition'
}).use(['form', 'layer', 'jquery', 'table', 'laydate', 'upload', 'dynamicCondition'], function () {
    let form = layui.form,
        layer = layui.layer,
        laypage = layui.laypage,
        $ = layui.jquery,
        table = layui.table,
        upload = layui.upload,
        laydate = layui.laydate,
        dynamicCondition = layui.dynamicCondition;

    setTimeout(function() {
        table.render({
            elem: '#table'
            ,title: '列表'
            ,url: '/manage/common/list/duty'
            ,method: 'post'
            ,height: 'full-100'
            ,page: true //开启分页
            ,limit: 20
            ,limits: [20, 30, 40, 50, 60, 70, 80, 90, 100]
            ,loading: true
            ,cols: [object.cols]
            ,text: {
                none: '暂无相关数据' //默认：无数据。注：该属性为 layui 2.2.5 开始新增
            }
            ,toolbar: '#tableToolbar'
            ,defaultToolbar: ['filter']
        });

        var dataFields = object.dataFields;

        top_dc = undefined;

        dc = dynamicCondition.create({
            fields: dataFields
            ,tableId: "table"
            ,conditionTextId: "#findMsg"
            ,type: "complex"
            ,requestDataType: "json"
            ,counts: 10
            ,queryCallBack: function (requestData) {
            }
            ,afterOpen: function (_dc) {
                top_dc = _dc;
            }
        });
    }, 1000);

    table.on('toolbar(table)', function(obj){
        var tableCheckData = table.checkStatus(obj.config.id);
        switch (obj.event) {
            case 'find':
                dc.open();
                break;
            case 'upd':
                var data = tableCheckData.data;
                if (data.length == 0) {
                    layer.msg("请选择要修改的数据");
                    return;
                }
                ;
                if (data.length != 1) {
                    layer.msg("请选择单条数据");
                    return;
                }
                ;
                var index = layer.open({
                    type: 2
                    ,title: '修改'
                    ,area: ['50%', '100%']
                    ,content: '/manage/common/editHtml/' + object.url + '/' + data[0].id
                    ,btn: ['保存', '取消']
                    ,offset: 'r'
                    ,yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        var iframeWindow = $(layero).find("iframe")[0].contentWindow;
                        var d = iframeWindow.getFormDataAndValidate();
                        if (!d[0]) {
                            return;
                        }
                        var master = d[1];
                        master.id = data[0].id;

                        var shadeIndex = top.layer.msg('数据提交中，请稍候', {icon: 16, time: false, shade: 0.4});
                        $.post("/manage/common/update", {url: object.url, obj: JSON.stringify(master)}, function (d) {
                            if (d.code == '200') {
                                top.layer.msg("修改成功", {icon: 1});
                                table.reload('table');
                            } else {
                                top.layer.msg("修改失败," + d.msg, {icon: 2});
                            }
                            top.layer.close(shadeIndex);
                            layer.close(index);
                        }, "json")
                    }
                });
                break;
        };
    });

})